﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LibarayMS
{
    public partial class Frm_book_info : Form
    {
        private int brid;
        private string mname;
        //用于判断库存只能增加不能减少
        private int oldtotal;
        public event myDelegate returnEvent;
        public event myDelegate logoutEvent;
        public Frm_book_info(string mname)
        {
            this.mname = mname;
            InitializeComponent();
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            returnEvent();
            this.Close();
        }

        private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            logoutEvent();
            this.Close();
        }

        protected void clearTextBox()
        {
            txt_ISBN.Text = "";
            txt_BName.Text = "";
            txt_author.Text = "";
            txt_Ename.Text = "";
            txt_edit_ID.Text = "";
            dt_pub_time.Value = DateTime.Now.Date;
            nu_total.Value = 0;
            nu_price.Value = 0;
            txt_abstract.Text = "";
            lbl_status.Text = "添加";
        }

        private void btn_save_Click(object sender, EventArgs e)
        {
            string ISBN=txt_ISBN.Text.Trim();
            string BName=txt_BName.Text.Trim();
            string author=txt_author.Text.Trim();
            string EName=txt_Ename.Text.Trim();
            string edit_ID=txt_edit_ID.Text.Trim();
            string edit_time=dt_pub_time.Value.ToString("yyyy-MM-dd");
            int total_num=((int)nu_total.Value);
            int price=((int)nu_price.Value);
            string Abstract=txt_abstract.Text.Trim();

            if (ISBN == "")
            {
                lbl_note.Text = "ISBN不能为空!";
                lbl_note.ForeColor=Color.Red;
                txt_ISBN.Focus();
            }
            else if (BName == "")
            {
                lbl_note.Text = "书名不能为空!";
                lbl_note.ForeColor = Color.Red;
                txt_BName.Focus();
            }
            else if (EName == "")
            {
                lbl_note.Text = "出版社名称不能为空!";
                lbl_note.ForeColor = Color.Red;
                txt_Ename.Focus();
            }
            else if (edit_ID == "")
            {
                lbl_note.Text = "版次不能为空!";
                lbl_note.ForeColor = Color.Red;
                txt_edit_ID.Focus();
            }
            else if (total_num == 0)
            {
                lbl_note.Text = "库存总量不能为空!";
                lbl_note.ForeColor = Color.Red;
                nu_total.Focus();
            }
            else if (price == 0)
            {
                lbl_note.Text = "价格不能为空!";
                lbl_note.ForeColor = Color.Red;
                nu_price.Focus();
            }
            else if (lbl_status.Text == "添加")
            {
                MySqlConnection conn = new MySqlConnection("Data Source=localhost;database=my_db1;UID=root;PWD=123456");
                conn.Open();
                MySqlCommand cmd = new MySqlCommand("addBook", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                //定义输入函数
                cmd.Parameters.AddWithValue("isbn_", ISBN);
                cmd.Parameters.AddWithValue("bname_", BName);
                cmd.Parameters.AddWithValue("author_", author);
                cmd.Parameters.AddWithValue("ename_", EName);
                cmd.Parameters.AddWithValue("editid_", edit_ID);
                cmd.Parameters.AddWithValue("etime_", edit_time);
                cmd.Parameters.AddWithValue("totalnum_", total_num);
                cmd.Parameters.AddWithValue("price_", price);
                cmd.Parameters.AddWithValue("abstract_", Abstract);
                //定义输出函数
                MySqlParameter result = cmd.Parameters.Add("result", MySqlDbType.Int64);
                result.Direction = ParameterDirection.Output;
                cmd.ExecuteNonQuery();
                if (result.Value.ToString()=="1")
                {
                    lbl_note.Text = "恭喜你，添加成功！";
                    lbl_note.ForeColor = Color.Blue;
                    //trans.Commit();
                    //清空输入框
                    clearTextBox();
                    bind_book_info();
                }
                else
                {
                    lbl_note.Text = "对不起，添加失败！";
                    lbl_note.ForeColor = Color.Red;
                }
                conn.Close();
            }
            else
            {
                if (total_num < oldtotal)
                {
                    //库存只能增加不能减小
                    lbl_note.Text = "库存不能减少！";
                    lbl_note.ForeColor = Color.Red;
                    nu_total.Value = oldtotal;
                }
                else
                {
                    MySqlConnection conn = new MySqlConnection("Data Source=localhost;database=my_db1;UID=root;PWD=123456");
                    conn.Open();
                    MySqlCommand cmd = new MySqlCommand("updateBook", conn);
                    cmd.CommandType = CommandType.StoredProcedure;
                    //定义输入函数
                    cmd.Parameters.AddWithValue("brid_", brid);
                    cmd.Parameters.AddWithValue("isbn_", ISBN);
                    cmd.Parameters.AddWithValue("bname_", BName);
                    cmd.Parameters.AddWithValue("author_", author);
                    cmd.Parameters.AddWithValue("ename_", EName);
                    cmd.Parameters.AddWithValue("editid_", edit_ID);
                    cmd.Parameters.AddWithValue("etime_", edit_time);
                    cmd.Parameters.AddWithValue("totalnum_", total_num);
                    cmd.Parameters.AddWithValue("price_", price);
                    cmd.Parameters.AddWithValue("abstract_", Abstract);
                    //定义输出函数
                    MySqlParameter result = cmd.Parameters.Add("result", MySqlDbType.Int64);
                    result.Direction = ParameterDirection.Output;
                    cmd.ExecuteNonQuery();
                    if (result.Value.ToString() == "1")
                    {
                        lbl_note.Text = "恭喜你，修改成功！";
                        lbl_note.ForeColor = Color.Blue;
                        //清空输入框
                        clearTextBox();
                        bind_book_info();
                    }
                    else
                    {
                        lbl_note.Text = "对不起，修改失败！";
                        lbl_note.ForeColor = Color.Red;
                    }
                    conn.Close();
                }
            }
        }

        private void Frm_book_info_Load(object sender, EventArgs e)
        {
            lbl_mname.Text = mname;
            bind_book_info();
        }

        protected void bind_book_info()
        {
            MySqlConnection conn = new MySqlConnection("Data Source=localhost;database=my_db1;UID=root;PWD=123456");
            conn.Open();
            string str = "select a.BRID,ISBN,BName,author,abstract,total_amount,current_amount,EName,edit_ID,edit_date,price from book_info a,book_edit_info b,book_price_info c,book_date_info d,edit_house_info e where a.BRID=b.BRID and a.BRID=c.BRID and a.BRID=d.BRID and b.PHID=e.PHID;";
            MySqlDataAdapter msda = new MySqlDataAdapter(str, conn);
            DataTable dt = new DataTable();
            msda.Fill(dt);
            conn.Close();
            listView1.Items.Clear();
            foreach (DataRow dr in dt.Rows)
            {
                ListViewItem myitem = new ListViewItem(dr["ISBN"].ToString());
                myitem.SubItems.Add(dr["BName"].ToString());
                myitem.SubItems.Add(dr["author"].ToString());
                myitem.SubItems.Add(dr["EName"].ToString());
                myitem.SubItems.Add(dr["edit_ID"].ToString());
                myitem.SubItems.Add(Convert.ToDateTime(dr["edit_date"]).ToString("yyyy-MM-dd"));
                myitem.SubItems.Add(dr["total_amount"].ToString());
                myitem.SubItems.Add(dr["current_amount"].ToString());
                myitem.SubItems.Add(dr["price"].ToString());
                myitem.SubItems.Add(dr["abstract"].ToString());
                //隐藏列BRID
                myitem.SubItems.Add(dr["BRID"].ToString());
                listView1.Items.Add(myitem);
            }
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(listView1.SelectedItems.Count > 0)
            {
                ListViewItem myitem = listView1.SelectedItems[0];
                txt_ISBN.Text = myitem.SubItems[0].Text;
                txt_BName.Text = myitem.SubItems[1].Text;
                txt_author.Text = myitem.SubItems[2].Text;
                txt_Ename.Text = myitem.SubItems[3].Text;
                txt_edit_ID.Text = myitem.SubItems[4].Text;
                dt_pub_time.Value = DateTime.ParseExact(myitem.SubItems[5].Text, "yyyy-MM-dd", CultureInfo.InvariantCulture);
                nu_total.Value = int.Parse(myitem.SubItems[6].Text);
                nu_price.Value = int.Parse(myitem.SubItems[8].Text);
                txt_abstract.Text = myitem.SubItems[9].Text;
                lbl_status.Text = "修改";
                brid = int.Parse(myitem.SubItems[10].Text);
                oldtotal= int.Parse(myitem.SubItems[6].Text); ;
            }
        }

        private void btn_del_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count==0)
            {
                MessageBox.Show("请先选择要删除的书籍！");
            }
            else
            {
                DialogResult dResult = MessageBox.Show("确定要删除选择的书籍？", "删除提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dResult == DialogResult.Yes)
                {
                    MySqlConnection conn = new MySqlConnection("Data Source=localhost;database=my_db1;UID=root;PWD=123456");
                    conn.Open();
                    MySqlCommand cmd = new MySqlCommand("deleteBook", conn);
                    cmd.CommandType = CommandType.StoredProcedure;
                    //定义输入函数
                    cmd.Parameters.AddWithValue("brid_", brid);
                    //定义输出函数
                    MySqlParameter result = cmd.Parameters.Add("result", MySqlDbType.Int64);
                    result.Direction = ParameterDirection.Output;
                    cmd.ExecuteNonQuery();
                    if (result.Value.ToString() == "1")
                    {
                        lbl_note.Text = "恭喜你，删除成功！";
                        lbl_note.ForeColor = Color.Blue;
                        //清空输入框
                        clearTextBox();
                        bind_book_info();
                    }
                    else
                    {
                        lbl_note.Text = "对不起，删除失败！";
                        lbl_note.ForeColor = Color.Red;
                    }
                    conn.Close();
                }
            }
        }

        private void btn_cancel_Click(object sender, EventArgs e)
        {
            clearTextBox();
            lbl_note.Text = "";
        }
    }
}
